package com.flejay.dreamrecruiter.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Named;
import javax.inject.Singleton;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flejay.dreamrecruiter.service.ILoggingService;

@Singleton
@Named("logService")
public class LoggingService implements ILoggingService {

private final Map<Class<?>, Logger> loggers = new HashMap<Class<?>, Logger>();
	
	@Override
	public void debug(Class<?> clazz, String message, Object... objects) {
		Logger logger = checkAndInitLogger(clazz);
		if (logger.isDebugEnabled()) {
			logger.debug(message, objects);
		}
	}
	
	@Override
	public void error(Class<?> clazz, String message, Object... objects) {
		Logger logger = checkAndInitLogger(clazz);
		logger.error(message, objects);
	}
	
	@Override
	public void info(Class<?> clazz, String message, Object... objects) {
		Logger logger = checkAndInitLogger(clazz);
		logger.info(message, objects);
	}

	/**
	 * Methode qui verifie si le logger est connu. Si oui, elle le renvoie, si
	 * non elle le cree, l'initie puis le renvoie.
	 * 
	 * @return le logger associe � la classe.
	 */
	private Logger checkAndInitLogger(Class<?> clazz) {
		Logger logger = this.loggers.get(clazz);
		if (logger == null) {
			logger = LoggerFactory.getLogger(clazz);
			this.loggers.put(clazz, logger);
		}
		return logger;
	}
}
